<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%!
	// 新闻列表
	String list(Input input) {
		Map map = new HashMap();
		Integer page = 1; // 页码
		Integer itemCount = input.getInt("item_count", 20); // 每一页条目
		Integer category = input.getInt("category", 1); // 新闻类别
		Integer hospital = input.getInt("hospital", -1); // 医院id
		String branch = input.getDesc("branch", "-1"); // 医院分支  不传返回所有
		Integer tag = input.getInt("tag", -1); // 新闻标记, 区分视频3/附件2/普通新闻1/视频附件都有4   不传返回所有
		Integer labelId = input.getInt("label_id", -1); // 通过标签与新闻关联查询   不传默认通过category新闻类别查
		String sinceID = input.getDesc("since_id", "0"); // 下拉刷新
 		String maxID = input.getDesc("max_id", "0"); // 上拉加载
		Integer uuid = input.getInt("uid", -1);
		String order = "desc"; // 排序
		String newsId = "";
		String selectContent = "new map(n.id as id , n.author as author, n.newsId as news_id, n.url as url, n.title as title, n.summary as summary, n.time as time, n.img as img, n.jsonString as json, n.tag as tag, n.category as category, n.branchName as branch_name)";
		String table = "News n";
		if (!sinceID.equals("0") && !sinceID.equals("") ) {
			newsId = "n.newsId > " + sinceID;
			order = "asc";
		} else if (!maxID.equals("0") && !maxID.equals("")) {
			newsId = "n.newsId < " + maxID;
		}
		Where where = new Where();
		if (!branch.equals("-1")) {
			where.and("n.branch=", branch);
		}
		if (tag != -1) {
			where.and("n.tag=", tag);
		}
		if (labelId == -1) {
			where.and("n.category=", category);
		} else {
			table += ", LabelRelation lr";
			where.and("lr.labelId=", labelId).and("lr.targetId=n.id").and("lr.status=", STATUS_ENABLE);
		}
		List<Map> newsList = QBuilder.db("basedb").select(selectContent).from(table).where(where.and("n.hospital=", hospital).and("n.status = ", STATUS_ENABLE).and(newsId)).order("n.time", order).page(itemCount, page).query().getResultObj();
		if (newsList.size() == 0) {
			Message msg = new Message(30041, "暂无相关数据");
			msg.put("data", new ArrayList());
			return msg.toJson();
		}
		for( Map maptest : newsList){

			maptest.put("img",(List)JSON.parse(maptest.get("img").toString().replaceAll("\r","").replaceAll("\n","")));
			maptest.put("json",(Map)JSON.parse(maptest.get("json").toString().replaceAll("\r","").replaceAll("\n","")));
		}
		if (order.equals("asc")) { // 上拉加载时倒序
			Collections.reverse(newsList);
		}
		Message msg = new Message(0);
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(NEWS_CATEGORY);
		oh.setOperate(LOVE);
		msg.put("data", love(query(newsList, oh), uuid, oh));
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, list(input));
%>
